package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.qo.QueryObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/list")
    public String list(QueryObject queryObject, Model model){
        PageInfo<Role> pageResult = roleService.selectByQuery(queryObject);
        //List<Role> list = roleService.getAll();
        model.addAttribute("result",pageResult);
        return "role/list";
    }



    @RequestMapping("/input")
    public String input(Long id, Model model){
        if(id != null){
            Role role = roleService.getRoleById(id);
            model.addAttribute("entity",role);
        }
        model.addAttribute("permissions", permissionService.getAll());
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        roleService.saveOrUpdate(role,ids);
        return "redirect:/role/list.do";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            roleService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }



}
